package com.su02.multi.data;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.tuple.Pair;

public class Matrix extends ArrayListDelegator<Vector> {
    private int nColumns = -1;
    public Matrix(Iterable<Vector> vectors) {
        int nVector = 0;
        int sizeVector = -1;
        for (Vector vector : vectors) {
            if (sizeVector == -1) {
                sizeVector = CollectionUtils.size(vector);
            } else if (sizeVector != CollectionUtils.size(vector)) {
                throw new IllegalArgumentException();
            }
            this.add(vector);
            nVector ++;
        }
        if (nVector == 0) {
            throw new IllegalArgumentException();
        }
        nColumns = sizeVector;
    }

    public Matrix() {
        initialize(10);
    }

    public Pair<Integer, Integer> shape() {
        if (nColumns == -1) {
            throw new IllegalStateException();
        }
        return Pair.ofNonNull(size(), nColumns);
    }

    @Override
    public boolean add(Vector vec) {
        if (this.nColumns == -1) {
            this.nColumns = CollectionUtils.size(vec);
        } else if (this.nColumns != CollectionUtils.size(vec)) {
            throw new IllegalArgumentException();
        }
        if (this.nColumns == 0) {
            throw new IllegalArgumentException();
        }
        return super.add(vec);
    }
}
